﻿@using System.Data.Entity
@using Microsoft.Ajax.Utilities
@model QAS.Nekorama.Models.Tag
@using QAS.Nekorama.Common
@using Resources
@{
    ViewBag.Title = QAS_Nekorama.NEW_QUESTION;
}
<script src="~/Scripts/jquery-te-1.4.0.min.js"> </script>
<link href="~/Content/jquery-te-1.4.0.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui-1.8.20.js"> </script>

<script src="~/Scripts/jquery.validationEngine.js"> </script>
<script src="@QAS_Nekorama.VALID_ENGINE"> </script>

<script>
    $(function() {
        var availableTags = [];

        function split(val) {
            return val.split(/,\s*/);
        }

        function extractLast(term) {
            return split(term).pop();
        }

        $("#alltag")
            // don't navigate away from the field on tab when selecting an item
            .bind("keyup", function(event) {
                $.ajax({
                    url: '@Url.Action("GetTags", "Question")',
                    dataType: 'json',
                    data: { tags: $("#alltag").val() },
                    success: function(result) {
                        availableTags = result["result"];
                        var tags = $("#alltag").val().split(',');
                        $.each(tags, function(key, value) {
                            var index = $.inArray(value.trim(), availableTags);
                            if (index >= 0) {
                                availableTags.splice(index, 1);
                            }
                        });
                        if ($("#alltag").val().split(", ").length > 6) {
                            $("#message").validationEngine('showPrompt', '@Messages.TAG_LIMIT', 'error', 'topRight');
                        } else {
                            $("#message").validationEngine('hide');
                        }
                    },
                    error: function(xhr) {
                        alert(xhr.statusText);
                    }
                });
                if (event.keyCode === $.ui.keyCode.TAB &&
                    $(this).data("ui-autocomplete").menu.active) {
                    event.preventDefault();
                }
            })
            .autocomplete({
                minLength: 0,
                source: function(request, response) {
                    // delegate back to autocomplete, but extract the last term
                    response($.ui.autocomplete.filter(
                        availableTags, extractLast(request.term)));
                },
                focus: function() {
                    // prevent value inserted on focus
                    return false;
                },
                select: function(event, ui) {
                    var terms = split(this.value);
                    // remove the current input
                    terms.pop();
                    // add the selected item
                    terms.push(ui.item.value);
                    // add placeholder to get the comma-and-space at the end
                    terms.push("");
                    this.value = terms.join(", ");
                    if ($("#alltag").val().split(", ").length > 6) {
                        $("#message").validationEngine('showPrompt', '@Messages.TAG_LIMIT', 'error', 'topRight');
                    } else {
                        $("#message").validationEngine('hide');
                    }
                    return false;
                }
            });
    });

    $(document).ready(function() {
        $("#new_q_form").validationEngine('attach');
        $(".jqte_editor").blur(function() {
            checkdes();
        });
        $("#bounty").blur(function() {
            if ($("#bounty").val() != null && parseInt($("#bounty").val()) >= @Helper.GetReputation(WebSecurity.CurrentUserName)) {
                $("#bounty").validationEngine('showPrompt', '@Messages.NOT_ENOUGH_PERMISSION', 'error', 'topRight', true);
            } else {
                $("#bounty").validationEngine('hide');
            }
        });

    });

    function showMess(event) {
        event.preventDefault();
        var flag = false;
        if ($("#txt").val().replace(/&nbsp;/g, '').replace(/<br>/g, " ").trim().length < @Constants.QUESTION_CONTENT_MINIMUM)
            flag = true;
        if ($("#alltag").val().trim().split(", ").length > @Constants.NUMBER_TAG_MAXIMUM)
            flag = true;
        if ($("#title").val().trim().length < @Constants.QUESTION_TITLE_MINIMUM)
            flag = true;
        if ($("#bounty").val().trim() != null && parseInt($("#bounty").val()) >= @Helper.GetReputation(WebSecurity.CurrentUserName))
            flag = true;
        if (flag) {
            checkTag();
            checkdes();
            checkbt();
            $("#new_q_form").validationEngine('validate');
            event.preventDefault();
            return false;
        } else {
            $('#new_q_form').submit();
            return false;
        }
    }

    function checkTag() {
        if ($("#alltag").val().split(", ").length > @Constants.NUMBER_TAG_MAXIMUM) {
            $("#message").validationEngine('showPrompt', '@Messages.TAG_LIMIT', 'error', 'topRight', true);
        }
    }

    function checkdes() {
        if ($("#txt").val().replace(/&nbsp;/g, '').replace(/<br>/g, " ").trim().length < @Constants.QUESTION_CONTENT_MINIMUM) {
            $(".jqte_editor").validationEngine('showPrompt', '@Messages.QUESTION_CONTENT_LENGTH', 'error', 'topRight', true);
        } else {
            $(".jqte_editor").validationEngine('hide');
        }
    }

    function checkbt() {
        if ($("#bounty").val() != null && parseInt($("#bounty").val()) >= @Helper.GetReputation(WebSecurity.CurrentUserName)) {
            $("#bounty").validationEngine('showPrompt', '@Messages.NOT_ENOUGH_PERMISSION', 'error', 'topRight', true);
        } else {
            $("#bounty").validationEngine('hide');
        }
    }
</script>

<div id="primary">
    <div id="primary-1">
        <!--ERROR LIST-->
        @if (TempData["error"] != null)
        {
            <div class="error-list">
                <ul>
                    <li>@TempData["error"]</li>
                </ul>
            </div>
            <!--end error-list-->
        }
    </div>
    <!--end primary-1-->
    <form id="new_q_form" method="post" action="Newquestion">
        <div id="primary-2">
            <h2 style="line-height: normal; padding-top: 5px;">@QAS_Nekorama.TITLE</h2>
            <input type="text" class="validate[required],minSize[10]" id="title" name="title1" maxlength="225" value="@TempData["title"]" />
            <input type="text"  name="content" value="@TempData["content"]"  id="txt" />
            <script type="text/javascript"> $("#txt").jqte(); </script>
            <h2 style="line-height: normal; padding-top: 5px;">@QAS_Nekorama.BOUNTY (@QAS_Nekorama.OPTIONAL)</h2>

            @if (Helper.GetReputation(WebSecurity.CurrentUserName) >= (int) Constants.Privileges.SetBounty)
            {
                <input type="text" id="bounty" name="bounty" class="validate[custom[integer],min[100],max[1000]]" value="@TempData["bounty"]" />
                <br />
            }
            else
            {
                <input type="text" id="bounty" name="bounty" class="validate[custom[integer],min[100],max[1000]]" value="@TempData["bounty"]" disabled="disabled" />
                <br />
            }
            
            <input type="hidden" value="@Helper.GetReputation(WebSecurity.CurrentUserName)" id="hidenrep" />
            <h2 style="line-height: normal; padding-top: 5px;">@QAS_Nekorama.TAGS</h2>
            <div class="ui-widget">
                @Html.TextBoxFor(x => x.TagName, new {@id = "alltag", name = "tag", value = TempData["tag"], @class = "validate[required]", style = "border:1px solid #e8e8e8;border-radius:5px;font-size:12px;height:25px;margin-left:0px;padding-left:10px;width:200px;"})
                <input type="text" disabled="disabled" class="message" id="message" name="message" style="width: 0; color: transparent; border: none" />
            </div>
            <!--end ui-widget-->
        </div>
        <!--end primary-2-->

        <div id="primary-3">
            <div class="tools">
                @Html.ActionLink(QAS_Nekorama.POST, null, null, new {@class = "post-btn", @onclick = "showMess(event);"})
            </div>
            <!--end tools-->
        </div>
        <!--end primary-3-->
    </form>
</div>
<!--end primary-->